﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Threading;

namespace YaAppoint.BackgroundWorkers
{
    /// <summary>
    /// 清理过时数据(定时任务)
    /// </summary>
    public class AutoDeleteExpiredData: AsyncPeriodicBackgroundWorkerBase
    {
        public AutoDeleteExpiredData(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory)
            : base(timer, serviceScopeFactory)
        {
            Timer.Period = 60000; // 10分钟执行一次
        }

        //  执行任务内容
        protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
        {
            Logger.LogInformation("开始清理过期数据");

            // 从serviceProvider接口解析依赖注入
            // workerContext.ServiceProvider.GetRequiredService

            var departmentRepository = workerContext.ServiceProvider.GetRequiredService<IRepository<Department, Guid>>();

            await departmentRepository.FirstOrDefaultAsync();

            Logger.LogInformation("过期数据清理成功");
        }
    }
}
